package com.linewow.xhyy.nihaonew.view.input;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.linewow.xhyy.nihaonew.R;

/**
 * Created by LXR on 2016/9/2.
 */
public class PrimaryMenu extends PrimaryMenuBase implements View.OnClickListener {
    private LayoutInflater layoutInflater;
    private Button voiceBtn,imgBtn;
    private TextView sendBtn;
    private EditText msgEdit;
    private String TAG="PrimaryMenu";
    private static boolean voiceFlag;//确定是否 录音完成

    public PrimaryMenu(Context context) {
        super(context);
        init(context,null);
    }



    public PrimaryMenu(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public PrimaryMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }



    private void init(Context context, AttributeSet attrs) {
        layoutInflater= LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.input_primary,this);
        voiceBtn= (Button) findViewById(R.id.input_voice);
        imgBtn= (Button) findViewById(R.id.input_pic);
        sendBtn= (TextView) findViewById(R.id.input_go);
        msgEdit= (EditText) findViewById(R.id.input_edit);

        voiceBtn.setOnClickListener(PrimaryMenu.this);
        imgBtn.setOnClickListener(PrimaryMenu.this);
        imgBtn.setOnClickListener(PrimaryMenu.this);
        sendBtn.setOnClickListener(PrimaryMenu.this);
        msgEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.e(TAG,"beforeTextChanged内容"+charSequence+">i"+i+">"+i1+">"+i2);
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.e(TAG,"onTextChanged内容"+charSequence+">i"+i+">"+i1+">"+i2);
                listener.editChange();

            }

            @Override
            public void afterTextChanged(Editable editable) {
                Log.e(TAG,"afterTextChanged内容"+editable.toString());
            }
        });
    }

    public void prepareView(){
        msgEdit.setText("");
        msgEdit.setHint("");
    }

    public void resetHost(){
        msgEdit.setFocusable(true);
        msgEdit.requestFocus();
        InputMethodManager imm = (InputMethodManager)this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(msgEdit,0);
    }

    public void setReplyName(String name){
        msgEdit.setFocusable(true);
        msgEdit.setHint("to "+name);
    }


    @Override
    public void onClick(View view) {
        int id=view.getId();
        switch (id){
            case R.id.input_go:
                String str=msgEdit.getText().toString();
                if(null!=str){
                    listener.sendMessage(str);
                }
                Log.e(TAG,"发送信息了1"+str);
                break;
            case R.id.input_voice:
                listener.voiceClick();
                hideKeyBoard2();
                break;
            case R.id.input_pic:
                listener.imgClick();
                hideKeyBoard2();
                break;
        }
    }

    public void hideKeyBoard2(){
        InputMethodManager imm = (InputMethodManager)this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(msgEdit.getWindowToken(),0);
    }


    @Override
    public void pathFinish(boolean flag) {
        voiceFlag=flag;
        listener.sendPathMessage(voiceFlag);
    }

    @Override
    public void commitFinish(String path) {
        listener.sendPath(path);
    }



}
